package com.whd.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whd.domain.ResultVo;
import com.whd.entity.Foot;
import com.whd.entity.User;
import com.whd.service.FootService;
import com.whd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author whd
 * @since 2023-12-09
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private FootService footService;

    @PostMapping("register")
    public ResultVo register(
            String name,
            String username,
            String password
    ) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        List<User> list = userService.list(queryWrapper);
        if (!list.isEmpty())return ResultVo.failure("已存在此用户账户");
        User user = new User();
        user.setUsername(username);
        user.setName(name);
        user.setPassword(password);
        user.setToken(UUID.randomUUID().toString());
        userService.save(user);
        return ResultVo.success("注册成功",user.getToken());
    }
    @PostMapping("login")
    public ResultVo login(
            String username,
            String password
    ) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("password", password);
        User user = userService.getOne(queryWrapper);
        if (user != null) {
            return ResultVo.success("登录成功",user.getToken());
        }
        return ResultVo.failure("账户密码错误");
    }
    @PostMapping("save")
    public ResultVo saveFoot(String footName,String token){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("token",token);
        User one = userService.getOne(queryWrapper);
        if (one==null){
            return ResultVo.failure();
        }
        Foot foot = new Foot();
        foot.setFoot(footName);
        foot.setUserId(one.getId());
        footService.save(foot);
        return ResultVo.success("添加成功");
    }
    @PostMapping("find")
    public ResultVo find(String token){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("token",token);
        User one = userService.getOne(queryWrapper);
        if (one==null){
            return ResultVo.failure();
        }
        QueryWrapper<Foot> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id",one.getId());
        List<Foot> list = footService.list(queryWrapper1);
        return ResultVo.success("查询成功", list);
    }
    @PostMapping("delete")
    public ResultVo delete(String footName,String token){
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("token",token);
        User one = userService.getOne(queryWrapper1);
        if (one==null){
            return ResultVo.failure();
        }
        QueryWrapper<Foot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("foot",footName).eq("user_id",one.getId());
        List<Foot> list = footService.list(queryWrapper);
        List<Integer> integers = list.stream().map(foot -> foot.getId()).collect(Collectors.toList());
        footService.removeByIds(integers);
        return ResultVo.success("删除成功");
    }
}

